package com.swipemaster.levelpage;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.swipemaster.Constants;
import com.swipemaster.R;
import com.swipemaster.achievements.LevelsInfo;

public class LevelSelectorAdapter extends BaseAdapter
{
	private Activity mActivity;
	private LevelsInfo mLevelsInfo;

	public LevelSelectorAdapter(Activity activity, LevelsInfo levelsInfo)
	{
		mActivity = activity;
		mLevelsInfo = levelsInfo;
	}

	public Object getItem(int position)
	{
		return null;
	}

	public long getItemId(int position)
	{
		return 0;
	}

	/**
	 * Create a new RelativeLayout with TextView and Image for each item referenced by the Adapter.
	 */
	public View getView(int position, View convertView, ViewGroup parent)
	{
		RelativeLayout layout;
		if (convertView == null)
		{
			// If it's not recycled, initialize some attributes.
			LayoutInflater inflater = LayoutInflater.from(mActivity.getApplicationContext());
			layout = (RelativeLayout) inflater.inflate(R.layout.level_selector, null);
			layout.setLayoutParams(new GridView.LayoutParams(45, 45));
			layout.setPadding(5, 5, 5, 5);
		}
		else
		{
			layout = (RelativeLayout) convertView;
		}
		setLevelNumber(layout, position);
		setStarsImage(layout, position);
		return layout;
	}

	private void setLevelNumber(RelativeLayout relativeLayout, int position)
	{
		TextView textView = (TextView) relativeLayout.findViewById(R.id.levelNumber);
		boolean locked = mLevelsInfo.getInfo(position).isLocked();
		if (locked)
		{
			textView.setBackgroundResource(android.R.drawable.ic_lock_lock);
			textView.setText("");
		}
		else
		{
			textView.setBackgroundResource(0);
			textView.setText(position + 1 + "");
		}
	}

	private void setStarsImage(RelativeLayout relativeLayout, int position)
	{
		ImageView imageView = (ImageView) relativeLayout.findViewById(R.id.stars);
		int stars = mLevelsInfo.getInfo(position).getStars();
		imageView.setImageResource(Constants.getStarsResource(stars));
	}

	@Override
	public int getCount()
	{
		return Constants.LEVELS;
	}
}